package com.example.imchat.controller.activity;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.imchat.R;
import com.example.imchat.controller.fragment.ChatFragment;
import com.example.imchat.controller.fragment.ContactListFragment;
import com.example.imchat.controller.fragment.SettingFragment;

public class MainActivity extends FragmentActivity {

    private RadioGroup rg_main;
    private  ChatFragment chatFragment;
    private ContactListFragment contactListFragment;
    private SettingFragment settingFragment;
    private RadioButton rb_chat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
        initView();

        initData();

        initListener();
    }

    private void initListener() {
        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Fragment fragment = null;

                switch (checkedId){
                    //会话界面
                    case R.id.rb_chat:
                        fragment = chatFragment;
                        break;

                        //联系人界面
                    case  R.id.rb_person:
                        fragment = contactListFragment;
                        break;

                        //设置界面
                    case R.id.rb_setting:
                        fragment = settingFragment;
                        break;
                }
                //实现fragment切换的方法
                switchFragment(fragment);
            }
        });

        //默认选择会话页面
        rg_main.check(R.id.rb_chat);
    }

    private void switchFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fl_main,fragment).commit();



    }

    private void initData() {
        //创建三个fragmen对象
         chatFragment = new ChatFragment();
         contactListFragment = new ContactListFragment();
         settingFragment = new SettingFragment();

    }

    private void initView() {
        rg_main = (RadioGroup)findViewById(R.id.rg_main);
        rb_chat = findViewById(R.id.rb_chat);


    }
}
